package edu.gdin.ecommerce.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.Customer;
import edu.gdin.ecommerce.service.ICustomerService;
import edu.gdin.ecommerce.util.Pager;

@Controller("customerAction")
@Scope("prototype")
public class CustomerAction extends ActionSupport{

	private String msgReturn;
	private Customer customer = new Customer();
	private ICustomerService customerService;
	private String from;
	private String to;
	private Pager pager;
	private Integer currentPage;
	
	private String result;
	
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public String getMsgReturn() {
		return msgReturn;
	}
	public void setMsgReturn(String msgReturn) {
		this.msgReturn = msgReturn;
	}
	public Customer getCustomer() {
		return customer;
	}
	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	public String addCustomerAction(){
		customer.setCreateTime(new Date());
		if(customerService.addCustomer(customer))
			return this.queryCustomerAction();
		else{
			this.addFieldError("msg", "该客户证件号已存在，无法添加客户!");
			setMsgReturn("该客户证件号已存在，无法添加客户!");
			return "customerAdd";
		}
	}
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String findCustomerAction(){
		Customer c = customerService.getCustomerById(customer.getCustomerId());
		setCustomer(c);
		return "customerModify";
	}
	
	
	public String updateCustomerAction(){
		
			customer.setUpdateTime(new Date());
			if(customerService.updateCustomer(customer))
				return this.queryCustomerAction();
			else{
				setMsgReturn("出错，请稍后再试");
				return "customerModify";
			}
	}
		
	public String queryCustomerAction(){
		Integer cPage = currentPage == null || currentPage < 1 ? 1 : currentPage;
		setPager(this.customerService.queryCustomer(customer, from, to, cPage));
		return "customerList";
	}
	
	@SuppressWarnings("unchecked")
	public String queryCustomerAjaxAction(){
		Integer cPage = currentPage == null || currentPage < 1 ? 1 : currentPage;
		setPager(this.customerService.queryCustomer(customer, from, to, cPage));
		List<Customer> list = (List<Customer>) pager.getList();
		
		JsonConfig config = new JsonConfig();
		config.setExcludes( new String[]{"birthday","areaProvince",
				"createTime","areaCity","areaDistrict",
				"updateTime","address","customerDesc",
				"postcode","tel","mobile"});  
		
		JSONArray array = JSONArray.fromObject(list, config);
		result = array.toString();
		return "success";
	}
	
}
